{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "### 模型v3预测\n",
    "    特征：+指数特征\n",
    "    首次涨停模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import time,datetime\n",
    "from datetime import date\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import log_loss\n",
    "import lightgbm as lgb\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n",
    "execfile('v4/ym_util.py')\n",
    "execfile('v4/ma.py')\n",
    "execfile('v4/predict_show.py')\n",
    "\n",
    "# 获取当前日期\n",
    "today_str = date.today().strftime(\"%m_%d\")\n",
    "out_path = 'output/'\n",
    "print(today_str)\n",
    "\n",
    "date_str = '2025-08-27'\n",
    "print(date_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "predict_df = pd.read_csv('output/v4_predict.csv')\n",
    "p_col(predict_df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# list(predict_df.columns)\n",
    "# predict_df[predict_df['date'] =='2025-08-07'][['date', 'code', 'float_mv','open_gap_ratio']].sort_values(by=['open_gap_ratio'])\n",
    "# predict_df\n",
    "# predict_df[predict_df['date']=='2025-08-22']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "### 一、10cm首板预测\n",
    "    阶段1概率顺位排序（高召回率 + 低精准率）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "zt_gt3_df = show_10cm_zt3_low_precent(predict_df)[['date','code','name','open','low','close','high','open_pct','low_pct','zt10']]\n",
    "zt_gt3_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('v4/predict_show.py')\n",
    "zt_2_df,today_zt2_df = show_10cm_zt2_low_precent(predict_df)\n",
    "today_zt2_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('v4/predict_show.py')\n",
    "show_zt2_low_pct(zt_2_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('v4/predict.py')\n",
    "f_10_type1_final = show_10_type1(predict_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# date_str='2025-06-05'\n",
    "tmp_10 = f_10_type1_final[f_10_type1_final['date']==date_str].reset_index(drop=True).round(3)\n",
    "# tmp_10 = f_10_type1_final[f_10_type1_final['date']=='2025-04-25'].reset_index(drop=True).round(3)\n",
    "# tmp_10 = f_10_type1_final[f_10_type1_final['date']=='2025-06-18'].reset_index(drop=True).round(3)\n",
    "# tmp_10 = f_10_type1_final[f_10_type1_final['date']=='2025-06-19'].reset_index(drop=True).round(3)\n",
    "\n",
    "# tmp_10 = f_10_type1_final[f_10_type1_final['date']=='2025-07-01'].reset_index(drop=True).round(3)\n",
    "# tmp_10 = f_10_type1_final[f_10_type1_final['date']=='2025-07-30'].reset_index(drop=True).round(3)\n",
    "\n",
    "# tmp_10 = f_10_type1_final[f_10_type1_final['date']=='2025-08-01'].reset_index(drop=True).round(3)\n",
    "# tmp_10 = f_10_type1_final[f_10_type1_final['date']=='2025-08-08'].reset_index(drop=True).round(3)\n",
    "\n",
    "\n",
    "tmp_10.sort_values(by=['fused_prob'],ascending=False).reset_index(drop=True)[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f_10_type1_final[f_10_type1_final['code']=='601003.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f_10_type1_fina l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "### 二、GT4预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('v4/predict.py')\n",
    "p_gt4_df = predict_gt4(predict_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predict_df[predict_df['code']=='000501.SZ'][['date','code','gt4']]\n",
    "# p_gt4_df[p_gt4_df['code']=='000501.SZ'][['date','code']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predict_df[predict_df['code']=='000501.SZ'][['date','code','gt4']][-10:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_gt4 = p_gt4_df[p_gt4_df['date']==date_str].reset_index(drop=True).round(3)\n",
    "# tmp_gt4 = p_gt4_df[p_gt4_df['date']=='2025-08-13'].reset_index(drop=True).round(3)\n",
    "tmp_gt4 = tmp_gt4[tmp_gt4['code'].astype(str).str.startswith(('00'))]\n",
    "\n",
    "tmp_gt4.sort_values(by=['fused_prob'],ascending=False).reset_index(drop=True)[:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predict_df[predict_df['code']=='600580.SH']\n",
    "# tmp_gt4[tmp_gt4['code']=='603222.SH']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18",
   "metadata": {},
   "source": [
    "#### 三、final_score打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "week_df = pd.read_csv('input/predict_v3_week.csv',index_col=0)\n",
    "p_col_len(week_df)\n",
    "maf = MaF()\n",
    "week_ma_df = maf.mark_ma_w(week_df)\n",
    "\n",
    "current_day_df = get_previous_days_data(predict_df,0)\n",
    "final_result_df = merge_all_df([week_ma_df,current_day_df])\n",
    "# print(list(final_result_df.columns))\n",
    "# final_result_df.to_csv('output/score_file/'+str(today_str)+'.csv',index=False,encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# final_result_df.columns\n",
    "# # pd.merge(week_ma_df, current_day_df,on=['date','code'])\n",
    "# week_ma_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "day_df = final_result_df\n",
    "start_pct = 0\n",
    "end_pct = 3\n",
    "day_df = day_df[day_df['quote_rate'] > start_pct]\n",
    "day_df = day_df[day_df['quote_rate'] < end_pct]\n",
    "# day_df = day_df[day_df['turnover'] > 0.5]\n",
    "day_df = day_df[day_df['turnover'] > 1]\n",
    "\n",
    "#二级筛选（次重要）\n",
    "day_df = day_df[day_df['float_mv'] < 500]\n",
    "day_df = day_df[(day_df['close'] < 60)&(day_df['close'] > 3 )]\n",
    "\n",
    "\n",
    "day_df = day_df[day_df['bias15'] >= 0]\n",
    "day_df = day_df[day_df['m_raise'] <= 31]\n",
    "\n",
    "# day_df = day_df[day_df['gt7'] > 0]\n",
    "day_df = day_df[day_df['w_duo'] == 1]\n",
    "\n",
    "day_df = pd.merge(stock_list,day_df,on='code')\n",
    "# day_df = day_df[day_df['code'].astype(str).str.startswith(('00','60'))]\n",
    "# day_df = day_df[day_df['code'].astype(str).str.startswith(('30','68'))]\n",
    "day_df = day_df[day_df['code'].astype(str).str.startswith(('00'))]\n",
    "\n",
    "\n",
    "day_df = day_df[['date', 'code','name','quote_rate', 'turnover','bias15','h_d20','h_d60','float_mv', 'raise_buy','w_raise','m_raise', 'r_d1_score', 'r_d10_score', 'final_score']]\n",
    "day_df = day_df.sort_values(by=['final_score'],ascending=False).reset_index(drop=True).round(2)\n",
    "split_index = 0\n",
    "day_df[20*split_index:20*(split_index+1)]\n",
    "# day_df[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "# day_df[day_df['code']=='300907.SZ']\n",
    "# day_df[day_df['code']=='688379.SH']\n",
    "# current_day_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "# day_df[['date','code','quote_rate','w_duo']]\n",
    "# print(list(final_result_df.columns))\n",
    "# predict_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(list(predict_df.columns))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import pandas as pd\n",
    "# import numpy as np\n",
    "# from datetime import timedelta\n",
    "\n",
    "# def calculate_weekly_ma(day_df, week_df):\n",
    "#     final_df = pd.DataFrame()\n",
    "#     tmp_day_df = day_df[['date', 'code','open', 'close', 'high', 'low',]]\n",
    "#     date_df = tmp_day_df.drop_duplicates(subset=['date'])\n",
    "#     for date_value in date_df['date']:\n",
    "#         tmp_w_df = week_df[week_df['date'] < date_value]\n",
    "#         tmp_d_df = tmp_day_df[tmp_day_df['date'] < date_value]\n",
    "#         tmp_final_df = pd.merge(tmp_d_df,tmp_w_df,on=['date','code'])\n",
    "#         print(date_value,len(tmp_d_df),len(tmp_w_df),len(tmp_final_df))\n",
    "#         final_df = pd.concat([final_df, tmp_final_df], ignore_index=True)  # 合并DataFrame\n",
    "\n",
    "\n",
    "#     return final_df\n",
    "        \n",
    "        \n",
    "\n",
    "# tmp_week_df = calculate_weekly_ma(predict_df,week_df)\n",
    "# tmp_week_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "# # tmp_week_df[tmp_week_df['date']=='2025-8-15']\n",
    "# # tmp_week_df[tmp_week_df['code']=='688819.SH']\n",
    "# single_df = predict_df[predict_df['code'].isin(['688108.SH','300870.SZ'])][['date', 'code','open', 'close', 'high', 'low',]]\n",
    "# single_df[single_df['date'] > '2025-07-30'].reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tmp_p_df = week_df[week_df['code'].isin(['688108.SH','300870.SZ'])][['date', 'code','open', 'close', 'high', 'low',]].reset_index(drop=True)\n",
    "\n",
    "# tmp_p_df[tmp_p_df['date'] > '2025-06-30']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
